package com.gitee.ed.arithmetic.leetcode.struct;

import java.util.StringJoiner;

/**
 * @author hongda.li
 */
public class ListNode {

    public int val;

    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static ListNode from(int... array) {
        ListNode head = new ListNode();
        ListNode temp = head;
        for (int i = 0, arrayLength = array.length; i < arrayLength; i++) {
            temp.val = array[i];
            if (i != arrayLength - 1) {
                temp.next = new ListNode();
                temp = temp.next;
            }
        }
        return head;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", "[", "]")
                .add("val = " + val)
                .add("next -> " + next)
                .toString();
    }
}
